﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using SmartMD.DataModel.Entity;
using System.ComponentModel;
using SmartMD.DataAccess.Service;
using SmartMD.Manager.Utils;
using Techstar.Dialog;

namespace SmartMD.Manager.Specimens
{
    /// <summary>
    /// Interaction logic for SearchResult.xaml
    /// </summary>
    public partial class PatientSearchResult : Window
    {
        public PatientSearchResult()
        {
            InitializeComponent();

            PatientInfoList = new ObservableCollection<PatientInfo>();
            ICollectionView view = CollectionViewSource.GetDefaultView(PatientInfoList);
            this.lvPatient.ItemsSource = view;
        }
        public PatientSearchResult(Container.SpecimenMsg _sm)
        {
            InitializeComponent();
            SM = _sm;
            PatientInfoList = new ObservableCollection<PatientInfo>();
            ICollectionView view = CollectionViewSource.GetDefaultView(PatientInfoList);
            this.lvPatient.ItemsSource = view;
        }

        public SpecStoreEditPad Owner = null;
        public Container.SpecimenMsg SM = null;

        private ObservableCollection<PatientInfo> _patientInfoList;
        public ObservableCollection<PatientInfo> PatientInfoList
        {
            get
            {
                return _patientInfoList;
            }
            set
            {
                _patientInfoList = value;
                ICollectionView view = CollectionViewSource.GetDefaultView(value);
                this.lvPatient.ItemsSource = view;
            }
        }

        private void Search_PreviewMouseMove(object sender, MouseEventArgs e)
        {

        }

        private void Search_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {

        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            PatientInfo patientInfo = (PatientInfo)this.lvPatient.SelectedItem;
            if (patientInfo == null)
            {
                string errMessage = "请选择病人信息！";
                System.Windows.MessageBox.Show(errMessage, @"提示信息", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            if (this.Owner != null)
            {
                this.Owner.DataSource.PatientId = patientInfo.Id;
            }
            if (SM != null)
            {
                SM.PatientName = patientInfo.Name;
            }
            this.Close();
        }

        private void BtnSearch_Click(object sender, RoutedEventArgs e)
        {
             ObservableCollection<PatientInfo> pInfos = SearchUtil.ForPatientInfos(txtSearch.Text);
             if (pInfos == null)
             {
                 TaskDialogEx.Warning("提示信息", "未查到您所要的患者信息！");
                // this.txtSearch.Text = "";
             }
             this.PatientInfoList = pInfos;
            /*if (string.IsNullOrEmpty(txtSearch.Text))
            {
                ObservableCollection<PatientInfo> tmpPatientInfoList = DataServiceManager.PatientInfoService.loadAllPatientInfos();
                PatientInfoList = tmpPatientInfoList;
            }
            else
            {              
                string[] keys = txtSearch.Text.Split(new[] { ':', '：' });
                if (keys.Length >= 2)
                {
                    ObservableCollection<PatientInfo> pInfos = DataServiceManager.PatientInfoService.GetFollowupInfos(keys[0], keys[1]);
                    this.PatientInfoList = pInfos;
                }
            }*/
        }

        private void TxtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                BtnSearch_Click(null, null);
        }
    }
}
